{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-17T14:10:35.541279Z",
     "start_time": "2024-12-17T14:10:34.508989Z"
    }
   },
   "source": [
    "ZHIPU_API_KEY = '65f85b27091a92ba8fbb32b1bcebfd6a.i6T4GfWNQM5aLF11'\n",
    "\n",
    "# 创建模型实例\n",
    "from langchain_openai import ChatOpenAI  # ChatOpenAI模型\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    temperature = 0.95,\n",
    "    model_name = \"glm-4-flash\",\n",
    "    openai_api_key = ZHIPU_API_KEY,\n",
    "    openai_api_base = \"https://open.bigmodel.cn/api/paas/v4/\"\n",
    ")"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T14:10:37.900067Z",
     "start_time": "2024-12-17T14:10:37.732783Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain import PromptTemplate\n",
    "from langchain.chains import LLMChain, SequentialChain\n",
    "\n",
    "# 第一个LLMChain：生成鲜花的介绍\n",
    "template = \"\"\"\n",
    "你是一个植物学家。给定花的名称和类型，你需要为这种花写一个200字左右的介绍。\n",
    "花名: {name}\n",
    "颜色: {color}\n",
    "植物学家: 这是关于上述花的介绍:\"\"\"\n",
    "prompt_template = PromptTemplate(\n",
    "    input_variables=[\"name\", \"color\"],\n",
    "    template=template\n",
    ")\n",
    "introduction_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=prompt_template,\n",
    "    output_key=\"introduction\"\n",
    ")"
   ],
   "id": "6c9eea24947ab27",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zxy\\AppData\\Local\\Temp\\ipykernel_38980\\1572919798.py:14: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
      "  introduction_chain = LLMChain(\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T14:10:39.459456Z",
     "start_time": "2024-12-17T14:10:39.456170Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第二个LLMChain：根据鲜花的介绍写出鲜花的评论\n",
    "template = \"\"\"\n",
    "你是一位鲜花评论家。给定一种花的介绍，你需要为这种花写一篇200字左右的评论。\n",
    "鲜花介绍:\n",
    "{introduction}\n",
    "花评人对上述花的评论:\"\"\"\n",
    "prompt_template = PromptTemplate(\n",
    "    input_variables=[\"introduction\"],\n",
    "    template=template\n",
    ")\n",
    "review_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=prompt_template,\n",
    "    output_key=\"review\"\n",
    ")"
   ],
   "id": "9ade5cf7da13e147",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T14:10:40.977438Z",
     "start_time": "2024-12-17T14:10:40.974832Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第三个LLMChain：根据鲜花的介绍和评论写出一篇自媒体的文案\n",
    "template = \"\"\"\n",
    "你是一家花店的社交媒体经理。给定一种花的介绍和评论，你需要为这种花写一篇社交媒体的帖子，300字左右。\n",
    "鲜花介绍:\n",
    "{introduction}\n",
    "花评人对上述花的评论:\n",
    "{review}\n",
    "社交媒体帖子:\n",
    "\"\"\"\n",
    "prompt_template = PromptTemplate(\n",
    "    input_variables=[\"introduction\", \"review\"],\n",
    "    template=template\n",
    ")\n",
    "social_post_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=prompt_template,\n",
    "    output_key=\"social_post_text\"\n",
    ")"
   ],
   "id": "7f546353a05cdc7d",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T14:11:26.499297Z",
     "start_time": "2024-12-17T14:11:10.890624Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 总的链：按顺序运行三个链\n",
    "overall_chain = SequentialChain(\n",
    "    chains=[introduction_chain, review_chain, social_post_chain],\n",
    "    input_variables=[\"name\", \"color\"],\n",
    "    output_variables=[\"introduction\", \"review\", \"social_post_text\"],\n",
    "    verbose=True\n",
    ")\n",
    "\n",
    "# 运行链并打印结果\n",
    "overall_chain({\n",
    "    \"name\": \"玫瑰\",\n",
    "    \"color\": \"黑色\"\n",
    "})"
   ],
   "id": "62fb8e84cade59ac",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001B[1m> Entering new SequentialChain chain...\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'name': '玫瑰',\n",
       " 'color': '黑色',\n",
       " 'introduction': '玫瑰，被誉为爱情之花，其颜色众多，其中黑色玫瑰尤其引人注目。这种黑色玫瑰，学名黑珍珠玫瑰，拥有深邃的紫色底色，边缘泛着黑色，呈现出一种神秘的美感。它的花瓣厚实，花期较长，不易凋谢。黑色玫瑰象征着神秘、高贵和神秘的爱情，是浪漫与优雅的象征。在园艺领域，它以其独特的魅力受到人们的喜爱。',\n",
       " 'review': '黑珍珠玫瑰，宛如夜空中最璀璨的星，以其深邃的紫色底色和边缘的黑色边缘，展现出令人沉醉的神秘之美。它的花瓣厚实、花期长久，既传递着浪漫的情感，又不失高雅气质。在爱情的世界里，它象征着神秘、高贵与不渝的爱情，是爱的最佳信使。在园艺界，它独特的魅力吸引了无数园艺爱好者的目光。若想在家中增添一份浪漫与高贵，黑珍珠玫瑰无疑是最佳选择。',\n",
       " 'social_post_text': '🌹✨ #浪漫之夜# 黑珍珠玫瑰，你心中的神秘浪漫使者！💍\\n\\n🌸 夜晚降临，一束黑珍珠玫瑰，仿佛夜空中最亮的星，点缀了爱的天空。它那深邃的紫色底色，边缘流转着神秘的黑光，每一朵都散发着高贵而深沉的气息。\\n\\n🌹 它的花瓣厚实而坚韧，花期长久，象征着爱情的坚贞与永恒。在这浪漫的花朵中，黑珍珠玫瑰不仅传递着激情的火花，更蕴含着无尽的优雅和高贵。\\n\\n🎁 在这个特别的日子里，用黑珍珠玫瑰表达你的爱意，让爱情如同这花朵般，神秘、高贵而不失浪漫。无论是送给心爱的人，还是在家中布置一个浪漫的角落，它都是最佳的选择。\\n\\n🌺 #黑珍珠玫瑰# #爱的信使# #园艺之美# 想要一份高贵与浪漫，就从黑珍珠玫瑰开始吧！🌹💐\\n\\n📍 花店地址：[您的花店地址]\\n📞 联系电话：[您的联系电话]\\n🛍️ 立即预定，让黑珍珠玫瑰成为你爱情的见证！🎉💖'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
